<?php
/****
** 收益列表    zm
** 
**/
class OrderinfoAction extends AuthAction{
	public static $db = array();
	//订单收益 zm
	public function index(){
//        var_dump($_GET);die;
        $where = array();
        if($_GET['start_time']){   //开始时间
            $star_time=strtotime($_GET['start_time']);
        }
        if($_GET['end_time']){   //结束时间
            $end_time = strtotime($_GET['end_time']);
        }
        if($star_time>0 && $end_time>0){
            $where['info.finnshed_time']=array('between',array($star_time,$end_time));
        }
	
        $where['info.pay_status']=2;// 支付状态；0，未付款；1，付款中 ；2，已付款
        $where['info.shipping_status']=2;//商品配送情况，0，未发货； 1，已发货；2，已收货；3，备货中
        $where['info.order_status']=1;//订单状态。0，未确认；1，已确 认；2，已取消；3，无效；4，退货；
	    $pre=C('DB_PREFIX');//表前缀
        $count=M()->table($pre.'g_order_info info')//
        ->join($pre.'member mem on info.user_id=mem.id')//
        ->where($where)//
        ->count();
        $page=D("Common")->getPage($count);//分页
        $field="info.*,mem.member_card";
    	$data =M()->table($pre.'g_order_info info')
    	 ->join($pre.'member mem on info.user_id=mem.id')//
         ->where($where)//
    	 ->field($field)->limit($page["start"].",".$page["pagesize"])->select();
		foreach($data as $k => $v){
			$data[$k]['order_amount']= sprintf("%.2f",$v['order_amount'])+ sprintf("%.2f",$v['shipping_fee']);//邮费
			$data[$k]['order_rebate']= sprintf("%.2f",$v['order_rebate']);
			$data[$k]['order_profit']= sprintf("%.2f",$v['order_profit']);
			$data[$k]['order_profit_all']= sprintf("%.2f",$v['order_profit_all']);

		}
    	 $list["list"]=$data;
    	 $list["page"]=$page["page"];
		$this->assign("list",$list);
         $where=array();
        if ($_GET['start_time']) {   //开始时间
            $star_time = strtotime($_GET['start_time']);
        }
        if ($_GET['end_time']) {   //结束时间
            $end_time = strtotime($_GET['end_time']);
        }
        if ($star_time > 0 && $end_time > 0) {
            $where['finnshed_time'] = array('between', array($star_time, $end_time));
        }
        $where['pay_status']=2;// 支付状态；0，未付款；1，付款中 ；2，已付款
        $where['shipping_status']=2;//商品配送情况，0，未发货； 1，已发货；2，已收货；3，备货中
        $where['order_status']=1;//订单状态。0，未确认；1，已确 认；2，已取消；3，无效；4，退货；
        $goodsorder_model=M('g_order_info');
        $this->finnshed_time=$goodsorder_model->where($where)->sum('finnshed_time');//完成时间
        $this->shipping_status=$goodsorder_model->where($where)->sum('shipping_status');//商品配送情况，0，未发货； 1，已发货；2，已收货；3，备货中
        $this->order_amount= sprintf("%.2f",$goodsorder_model->where($where)->sum('order_amount'));//应付款金额
        $this->order_rebate= sprintf("%.2f",$goodsorder_model->where($where)->sum('order_rebate'));//订单返利金额 返出去的
        $this->order_profit= sprintf("%.2f",$goodsorder_model->where($where)->sum('order_profit'));//订单利润= 应付款金额-订单返利
        $this->order_profit_all= sprintf("%.2f",$goodsorder_model->where($where)->sum('order_profit_all'));//订单返利总金额 返利池

		$this->display();
	}
    public function order_list(){
    	$order_info=M('g_order_info');
    	if (IS_POST) {
    		$_POST['start_time']=strtotime($_POST['start_time']);//选择时间段条件
    		if ($_POST['end_time']) {//无结束时间为当前时间
    			$_POST['end_time']=strtotime($_POST['end_time']);
    		}else{
    			$_POST['end_time']=time();
    		}
    		
    		$data=$order_info->where("finnshed_time>".$_POST['start_time']." and finnshed_time<".$_POST['end_time'])->select();
    	}else{
    		$data=$order_info->where(array('order_status'=>1))->select();
    	}
    	$date=array();
    	/* 遍历每个月的销量数 */
    	foreach ($data as $v){
    		$m=date('m',$v['finnshed_time']);
    		if ($m==11||$m==12) {/* 不查找11月12月的数据 */
    			continue;
    		}
    		if ($date[$m]['m']==$m) {
    			$date[$m]['s']+=1;
    		}else{
    			$date[$m]['m']=$m;
    			$date[$m]['s']=1;
    		}
    	}
    	
    	for ($i=1;$i<=12;$i++){
    		foreach ($date as $k=>$v){
    			if ($k==$i) {
    				$db[$k]=array();
    				array_push($db[$k],$v['m'],$v['s']);
    			}
    		}
    	}
    	foreach ($db as $v){
    		$da[]=$v[1];
    		$s[]=$v[0]."月";
    	}
    	$this->assign('da',$db);
    	$this->assign('db',json_encode($da));//传出json对象（销量）给模版的js语句
    	$this->assign('s',json_encode($s));//传出json对象（月份）给模版的js语句
    	$this->display();
    }
    public function zhi(){


    }
}
//echo strtotime('2015-05-15 15:19:34');